<?php
require_once "../core.php";
function Wx($postUrl){
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return json_decode($data);
}

function Get_access_token(){
    $arr=require "temp/access_token.php";
    if($arr==1){
        $arr=array("","0");
    }
    if(time()-$arr[1]>7190){
        $postUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6a26f16011095354&secret=d007ae1d5fcd5d757684346b572a8d72";
        $Token=Wx($postUrl);
        $arr[0]=$Token->access_token;
        $arr[1]=time();
        file_put_contents("temp/access_token.php","<?php return ".var_export($arr, true)."; ?>",LOCK_EX);
    }
    return $arr[0];
}

function GetUserInfo($openId){
    $access_token=Get_access_token();
    $postUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openId."&lang=zh_CN";
    return Wx($postUrl);
}

function GetTicket($state) {
    $token =Get_access_token();
    $postUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $token;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。

    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$state.'}}}');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));

    $result = curl_exec($ch);
    $arr=json_decode($result,true);
    if (isset($arr["errcode"])){
        file_put_contents("temp/access_token.php","",LOCK_EX);
        return "";
    }
    return $arr["ticket"];

}

function GetCodeImage($ticket){
    echo '<img src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticket.'">';
}


function SendMessage($user_id,$message) {
    global $core;
    $openId=$core->GetUserById($user_id)["weixin_Id"];
    $token =Get_access_token();
    $postUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $token;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。

    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, '{
    "touser":"'.$openId.'",
    "msgtype":"text",
    "text":
    {
         "content":"'.$message.'"
    }
}');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));

    $result = curl_exec($ch);
    //$arr=json_decode($result,true);
    return "";

}